#include "Components.h"

#include "Core/GlobalContext.h"

namespace SE
{
	//------------------------------------------------------------------------------------------------------------
	/* Tag component */
	TagComponent::TagComponent(const String& _tag) : tag(_tag)
	{
	}
	//------------------------------------------------------------------------------------------------------------

	//------------------------------------------------------------------------------------------------------------
	/* GUID component */
	GUIDComponent::GUIDComponent(const GUID& _guid) : guid(_guid)
	{
	}
	//------------------------------------------------------------------------------------------------------------

	//------------------------------------------------------------------------------------------------------------
	/* Transform component */
	TransformComponent::TransformComponent(
		const Vector3f& _position,
		const Vector3f& _rotation,
		const Vector3f& _scale)
		: position(_position), rotation(_rotation), scale(_scale)
	{
	}

	Matrix4f TransformComponent::getTransform() const
	{
		return glm::translate(Matrix4f(1.0f), position) *
			   glm::toMat4(Quaternion(rotation)) *
			   glm::scale(Matrix4f(1.0f), scale);
	}

	String TransformComponent::record() const
	{
		StringStream ss;
		ss << position.x << ' ' << position.y << ' ' << position.z << ' ';
		ss << rotation.x << ' ' << rotation.y << ' ' << rotation.z << ' ';
		ss << scale.x << ' ' << scale.y << ' ' << scale.z;

		return ss.str();
	}
	//------------------------------------------------------------------------------------------------------------

	//------------------------------------------------------------------------------------------------------------
	/* Camera component */
	Matrix4f CameraComponent::getProjection() const
	{
		auto backgroundSize = RenderAPI::GetBackgroundSize();
		auto aspectRatio = Float(backgroundSize.x) / Float(backgroundSize.y);
		if (CameraType::Orthographic == type)
		{
			return glm::ortho(
				-orthographicSize * aspectRatio * 0.5f,
				 orthographicSize * aspectRatio * 0.5f,
				-orthographicSize * 0.5f,
				 orthographicSize * 0.5f,
				 orthographicNear,
				 orthographicFar);
		}
		else
		{
			return glm::perspective(
				glm::radians(perspectiveFOV),
				aspectRatio,
				perspectiveNear,
				perspectiveFar);
		}
	}
	//------------------------------------------------------------------------------------------------------------
} // !namespace SE